package acm.pta14;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class 浪漫侧影 {
    static Tree root;
    static List<Integer> left = new LinkedList<>();
    static List<Integer> right = new LinkedList<>();
    static Map<Integer, Integer> indexMap = new HashMap<>();

    public static void main(String[] args) {
        Reader sc = new Reader();
        left.clear();
        right.clear();
        int n = sc.nextInt();
        int[] inOrder = new int[n];
        int[] postOrder = new int[n];
        for (int i = 0; i < n; i++) {
            inOrder[i] = sc.nextInt();
            indexMap.put(inOrder[i], i);
        }
        for (int i = 0; i < n; i++) {
            postOrder[i] = sc.nextInt();
        }
        root = build(inOrder, postOrder, 0, 0, n);
        levelOrder();
        System.out.print("R:");
        right.forEach(i -> System.out.print(" "+i));
        System.out.println();
        System.out.print("L:");
        left.forEach(i -> System.out.print(" "+i));
        System.out.println();
    }

    public static void levelOrder() {
        Queue<Tree> queue = new LinkedList<>();
        Tree curEnd = root;
        Tree nextEnd = root;
        queue.add(root);
        left.add(root.val);
        while (!queue.isEmpty()) {
            Tree cur = queue.poll();
//            if (curEnd==nextEnd){   //刚刚换层
//                left.add(cur.val);
//            }
            if (cur.left != null) {
                queue.add(cur.left);
                nextEnd = cur.left;
            }
            if (cur.right != null) {
                queue.add(cur.right);
                nextEnd = cur.right;
            }
            if (cur == curEnd) {      //抵达当前边界
                right.add(cur.val);
                if (!queue.isEmpty())left.add(queue.peek().val);
                curEnd = nextEnd;
            }
        }
    }
    //左中右，左右中
    public static Tree build(int[] inOrder,int[] postOrder,int is,int ps,int len){
        if (len<1){
            return null;
        }
        if (len==1){
            return new Tree(inOrder[is]);
        }
        Tree cur=new Tree(postOrder[ps+len-1]);
        int rootIndex=indexMap.get(cur.val);
        cur.left=build(inOrder,postOrder,is,ps,rootIndex-is);
        cur.right=build(inOrder,postOrder,rootIndex+1,ps+(rootIndex-is),len-(rootIndex-is)-1);
        return cur;
    }

    static class Tree{
        int val;
        Tree left;
        Tree right;

        public Tree(int val) {
            this.val = val;
        }
    }
    static class Reader {
        private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer tokenizer = new StringTokenizer("");

        private String innerNextLine() {
            try {
                return reader.readLine();
            } catch (IOException ex) {
                return null;
            }
        }

        public boolean hasNext() {
            while (!tokenizer.hasMoreTokens()) {    //内存中没有
                String nextLine = innerNextLine();
                if (nextLine == null) {     //且没有下一行
                    return false;
                }
                tokenizer = new StringTokenizer(nextLine);  //读入一行
            }
            return true;
        }

        public String nextLine() {
            tokenizer = new StringTokenizer("");    //清空内存中的字符
            return innerNextLine();
        }
        public void clear(){
            tokenizer = new StringTokenizer("");
        }

        public String next() {
            hasNext();
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble(){
            return Double.parseDouble(next());
        }
//    public BigInteger nextBigInt() {
//        return new BigInteger(next());
//    }
    }

}
